<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <style>
        .router-link-active {
            color: green;
        }
    </style>
</head>

<body>
    <div id="app">
        <h1>hello Router</h1>
        <p>
            <router-link to="/foo">Go to Foo</router-link>
            <router-link to="/bar">Go to Bar</router-link>
            <router-link to="/user/dushenyan/singup">Go to User </router-link>
        </p>
        <router-view></router-view>
        <router-view name="a"></router-view>
    </div>
    <script>
        const foo = {
            template: "<h3>I am Foo</h3>",
            watch: {
                $route: function (to, from) {
                    console.log("to", to)
                }
            }
        }
        const bar = {
            template: "<h3>I am Bar</h3>"
        }
        const user = {
            template: `
                <div>
                    <h2>I am User</h2>
                    <h3>I am User{{$route.params.id}}</h3>
                    <router-view></router-view>
                </div>
             `,
        }
        const singup = {
            template: `<h5>i am singup</h5>`
        }

        const down = {
            template: `<h5>i am down</h5>`
        }

        const routes = [{
                path: "/",
                // redirect: '/foo'
                redirect: to => {
                    console.log(to)
                }
            },
            {
                path: '/foo',
                components: {
                    default: foo,
                    a: bar
                }
            },
            {
                path: '/bar',
                component: bar
            },
            {
                path: "/user/:id/",
                name: "user",
                component: user,
                children: [{
                        path: "singup",
                        component: singup
                    },
                    {
                        path: "down",
                        component: down
                    }
                ]
            }
        ]
        const router = new VueRouter({
            routes,
            mode: 'history'
        })

        const app = new Vue({
            router
        })
        app.$mount('#app')
    </script>
</body>

</html>
